package com.bjpowernode.controller;

import com.bjpowernode.service.DynamicService;
import com.bjpowernode.service.FollowService;
import com.bjpowernode.service.UserService;
import com.bjpowernode.user.Dynamic;
import com.bjpowernode.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private DynamicService dynamicService;

    @Resource
    private FollowService followService;

    @RequestMapping(value = "/add.do")
    public ModelAndView addUser(@RequestParam("name") String username, String password,@RequestParam("type") Integer userType,String address){
        ModelAndView mv = new ModelAndView();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setUserType(userType);
        user.setAddress(address);
        int result = userService.addUser(user);
        if (result == 1){
            mv.setViewName("right");
        } else{
            mv.setViewName("error");
        }
        return mv;
    }

    @RequestMapping(value = "/login.do")
    public ModelAndView loginUser(@RequestParam("name") String username, String password){
        ModelAndView mv = new ModelAndView();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        boolean res = userService.loginUser(user);
        if (res){
            user = userService.loadUser(username);
            mv.addObject("username",username);
            mv.addObject("userType",user.getUserType());
            mv.setViewName("shouye");
        } else {
            mv.setViewName("loginError");
        }
        return mv;
    }

    @RequestMapping(value = "/logout.do")
    public String logoutUser(HttpServletRequest request){
        request.getSession().removeAttribute("username");
        request.getSession().removeAttribute("userType");
        return "logout";
    }

    @RequestMapping(value = "/findFans.do")
    public ModelAndView findFans(@RequestParam("name") String username){
        ModelAndView mv = new ModelAndView();
        List<User> userList = userService.findFans(username);
        mv.addObject("userList",userList);
        mv.setViewName("fansList");
        return mv;
    }
    @RequestMapping(value = "/findXinxi.do")
    public ModelAndView findXinxi(String name){
        ModelAndView mv = new ModelAndView();
        Integer dynamicSum = dynamicService.findDynamicSum(name);
        List<Dynamic> dynamicList = dynamicService.findSelfDynamic(name);
        Integer followSum = followService.findSelfFollow(name).size();
        Integer fansSum = userService.findFans(name).size();
        mv.addObject("dynamicSum",dynamicSum);
        mv.addObject("dynamicList",dynamicList);
        mv.addObject("followSum",followSum);
        mv.addObject("fansSum",fansSum);
        mv.addObject("userName",name);
        mv.setViewName("user");
        return mv;
    }

    @RequestMapping(value = "/shouye.do")
    public String shouye(){
        return "shouye";
    }
}
